package phone.qyg.com.qyg.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.base.tool.BaseActivity;
import com.app.config.ApplicationConfig;
import com.app.utils.CommonUtil;
import com.app.utils.JSONTool;
import com.app.utils.OptionImageUtils;
import com.app.utils.PromptManager;
import com.app.xutils.MyCallBack;
import com.app.xutils.Xutils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.HashMap;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.model.VolunteerBean;

/**
 * 志愿者详情
 */
@ContentView(R.layout.activity_volunteer_detail)
public class VolunteerDetailActivity extends BaseActivity {
    @ViewInject(R.id.back)
    TextView back;
    @ViewInject(R.id.iv_head)
    ImageView iv_head;
    @ViewInject(R.id.tv_name)
            TextView tv_name;
    @ViewInject(R.id.tv_sex)
            TextView tv_sex;
    @ViewInject(R.id.tv_content)
            TextView tv_content;
    @ViewInject(R.id.tv_province)
            TextView tv_province;
    @ViewInject(R.id.tv_political)
            TextView tv_political;
    String volunteer_id ;
    int type = 0;
    @Override
    public void initSucessView() {
        volunteer_id = getIntent().getStringExtra("volunteer_id");
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        type = getIntent().getIntExtra("type",0);
        if(type==0){
            back.setText("志愿者");
        }else{
            back.setText("群文人才");
        }
        getData();
    }

    private void getData() {
        HashMap<String,String> fMapKeys = new HashMap<String,String>();
        if(CommonUtil.isNetWork()){
            fMapKeys.put("volunteer_id",volunteer_id+"");
            Xutils.Get(ApplicationConfig.NET_DSN_PATH + ApplicationConfig.VOLUNTEER_DETAIL,
                    fMapKeys,
                    new MyCallBack<String>(){
                        @Override
                        public void onSuccess(String result) {
                            super.onSuccess(result);
                            VolunteerBean venues = JSONTool.jsonDefaluTranClazz(result,null, VolunteerBean.class);
                            Message message = new Message();
                            message.obj = venues;
                            message.what = 0;
                            mHandler.sendMessage(message);
                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {
                            super.onError(ex, isOnCallback);
                            PromptManager.showToast(R.string.network_error);
                        }
                    });
        }else{
            PromptManager.showToast(R.string.network_error);
        }
    }

    @Override
    public void initViewHandler() {

    }
    @Event(value = {R.id.back})
    private void viewOnClick(View v){
        switch (v.getId()){
            case R.id.back:
                this.finish();
                break;
        }
    }

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            VolunteerBean venues = (VolunteerBean) msg.obj;
            switch (msg.what) {
                case 0:
                    if(venues!=null){
                        setImgPath(venues.photo,iv_head,OptionImageUtils.getRectRatioAct());
                        tv_name.setText(venues.name);
                        tv_sex.setText(venues.sex);
                        tv_political.setText(venues.political+"");
                        tv_province.setText(venues.province+"");
                        tv_content.setText(venues.content+"");
                    }else{
                        PromptManager.showToast(R.string.network_error);
                    }
                    break;
            }
        }
    };
}
